home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_11 / pjp / osprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-02  |  1004 b   |  44 lines

  1. ---------- Listing 4: The file osprint.c ---------------
  2.  
  3. // osprint -- ostream::_Print(const char *, ...)
  4. #include <ctype.h>
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <ostream>
  9.  
  10. ostream& ostream::_Print(const char *code, ...)
  11.     {    // format data for inserter
  12.     _TRY_IO_BEGIN
  13.     if (!opfx())
  14.         setstate(badbit);
  15.     else
  16.         {    // build format and convert
  17.         char buf[_MAX_EXP_DIG + _MAX_SIG_DIG + 16];
  18.         char fmt[8];
  19.         char *s = fmt;
  20.         *s++ = '%';
  21.         if (code[0] != ' ' && flags() & showpos)
  22.             *s++ = '+';
  23.         if (code[0] == 'B' && flags() & showbase
  24.             || code[0] == 'P' && flags() & showpoint)
  25.             *s++ = '#';
  26.         if (code[1] == '.')
  27.             *s++ = '.', *s++ = '*';
  28.         if (code[2] != ' ')
  29.             *s++ = code[2];
  30.         *s = code[3];
  31.         if (flags() & uppercase && strchr("egx", *s) != 0)
  32.             *s = toupper(*s);
  33.         *++s = '\0';
  34.         va_list ap;
  35.         va_start(ap, code);
  36.         _Pad(code, buf, vsprintf(buf, fmt, ap));
  37.         va_end(ap);
  38.         }
  39.     osfx();
  40.     _CATCH_IO_END
  41.     return (*this);
  42.     }
  43.  
  44.